package com.mirstone.jetpackdemo;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.room.Room;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.mirstone.jetpackdemo.room.AppDataBase;

public class App extends Application {

    private static final String TAG = "App";
    private static AppDataBase db;

    public static AppDataBase getAppDataBase() {
        return db;
    }

    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            Log.d(TAG, "migrate: 数据库1升级到2");
            database.execSQL("ALTER TABLE user "
                    + " ADD COLUMN _car TEXT");
        }
    };
    static final Migration MIGRATION_2_3 = new Migration(2, 3) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            Log.d(TAG, "migrate: 数据库2升级到3");
            database.execSQL("ALTER TABLE user "
                    + " ADD COLUMN _dog TEXT");
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();

        db = Room.databaseBuilder(getApplicationContext(),
                AppDataBase.class, "jetpack")
                .allowMainThreadQueries()
                .addMigrations(MIGRATION_1_2, MIGRATION_2_3)
                .build();
    }
}
